package spring.mybatis;

import org.apache.ibatis.io.Resources;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
@ComponentScan("spring.mybatis.entity")
public class BatisConfiguration {
    @Bean
    public SqlSessionTemplate sqlSessionTemplate() throws IOException {
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
        return new SqlSessionTemplate(factory);
    }

    /**
     * @Bean   //单独创建一个Bean，方便之后更换
     *     public DataSource dataSource(){
     *         return new PooledDataSource("com.mysql.cj.jdbc.Driver",
     *                 "jdbc:mysql://localhost:3306/study", "root", "123456");
     *     }
     *
     *     @Bean
     *     public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){  //直接参数得到Bean对象
     *         SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
     *         bean.setDataSource(dataSource);
     *         return bean;
     *     }
     */
}
